import { getSpaceId, setSpaceId } from '@/utils/auth'
import { getProjectBySpaceId, getMenu } from '@/api/user'
import setTheme from '@/theme'

const state = {
  spaceId: getSpaceId(),
  object: {},
  roles: [],
  menuList: []
}

const mutations = {
  SET_SPACEID: (state, spaceId) => {
    setSpaceId(spaceId)
    state.spaceId = spaceId
  },
  SET_OBJECT: (state, object) => {
    state.object = object
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_MENUSLIST: (state, menuList) => {
    state.menuList = menuList
  }
}

const actions = {
  // user login
  getObject({ commit, state }) {
    return new Promise((resolve, reject) => {
      getProjectBySpaceId({ spaceId: state.spaceId }).then(response => {
        const data = response.result
        setSpaceId(data.spaceId)
        if (data.projectId) {
          commit('SET_ROLES', ['subProject'])
        } else {
          commit('SET_ROLES', ['mainProject'])
        }
        // if (process.env.NODE_ENV === 'production') {
        //   import(process.env.BASE_URL + 'theme/' + (data.code ? data.code : 'default') + '.css'),
        //   // require(process.env.BASE_URL + 'theme/' + (data.code ? data.code : 'default') + '.css')
        // } else {
        //   import '@/assets/theme/' + (data.code ? data.code : 'default') + '.css'
        // }
        setTheme((data.code ? data.code : 'default'))
        commit('SET_OBJECT', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

